package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.User;
import com.example.utils.ExceptionResult;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

import com.example.service.IPetsService;
import com.example.entity.Pets;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 宠物信息表 前端控制器
 * </p>
 *
 * @author dengc
 * @since 2023-05-10
 */
@CrossOrigin
@RestController
@RequestMapping("/pets")
public class PetsController {

        @Resource
        private IPetsService petsService;

        @PostMapping
        public ExceptionResult save(@RequestBody Pets pets) {
            return ExceptionResult.success(petsService.saveOrUpdate(pets));
        }

        @DeleteMapping("/{id}")
        public ExceptionResult delete(@PathVariable Integer id) {
            return ExceptionResult.success(petsService.removeById(id));
        }

        @GetMapping
        public ExceptionResult findAll() {
            return ExceptionResult.success(petsService.list());
        }


        @GetMapping("/{id}")
        public ExceptionResult findOne(@PathVariable Integer id) {
            return ExceptionResult.success(petsService.getById(id));
        }

        //批量删除
        @PostMapping("/del/batch")
        public ExceptionResult deleteBatch(@RequestBody List<Integer> ids){
                return ExceptionResult.success(petsService.removeByIds(ids));
        }

        @GetMapping("/page")
        public ExceptionResult findPage(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize,
                                        @RequestParam(defaultValue = "") String petsname,
                                        @RequestParam(defaultValue = "") String petstype) {
                LambdaQueryWrapper<Pets> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.like(!"".equals(petsname), Pets::getPetsname,petsname);
                if (!"".equals(petstype)){
                  Integer petstypeid = Integer.valueOf(petstype);
                  queryWrapper.eq(Pets::getPetstypeid,petstype);
                }
                return ExceptionResult.success(petsService.page(new Page<>(pageNum, pageSize),queryWrapper));
        }
}
